Skip to content

Nginx 安装(源码 · RPM · Windows)

分节介绍 源码 / RPM / Windows 三种安装方式;命令示例按教程收录,常见 nginx -t / reload 等在文末统一汇总。


安装方式一:源码包(Linux)

提示

源码包优势是高度可配置、跨发行版,但是缺点也很明显 非自动,配置起来很繁琐,推荐没有特殊需求,比如跨平台需求的话,其他情况都使用 rpm 包

安装 nginx

shell
# 安装相关依赖包
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel

# 创建 nginx 存储目录
cd /usr/local && mkdir nginx && cd nginx

# 下载 nginx 安装包
wget http://nginx.org/download/nginx-1.22.0.tar.gz

# 解压
tar -xvf nginx-1.22.0.tar.gz

# 编译
cd /usr/local/nginx/nginx-1.22.0
./configure
make && make install

# 启动 nginx 服务
cd /usr/local/nginx/sbin
./nginx

# 查看 nginx 是否成功启动
./nginx - t

配置 nginx 全局命令

shell
# 编辑全局环境变量配置文件
vim /etc/profile

# 添加内容:
PATH=$PATH:/usr/local/nginx/sbin
export PATH
# 保存并退出

# 重新加载配置文件
source /etc/profile

# 测试,在任意目录下执行
nginx -t

nginx 重要文件位置

shell
/usr/local/nginx/html/index.html # html 根目录
/usr/local/nginx/conf/nginx.conf # nginx 配置文件
/usr/local/nginx/sbin/nginx      # nginx 命令执行文件

修改 nginx 配置文件

shell
# 编辑配置文件
vim /usr/local/nginx/conf/nginx.conf
# 保存并退出

# 重启 nginx
nginx -s reload

# 查看状态是否正常
nginx -t

配置 nginx 开机自启

shell
# 修改 systemd 配置文件
vim /lib/systemd/system/nginx.service
# 添加如下规则
[Unit]

Description=nginx service

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true

[Install]

WantedBy=multi-user.target
# 保存并退出

# 关闭 selinux 否则报错
setenforce 0

# 有了以上配置,就可以通过 systemctl 来管理 nginx 服务了
启动 Nginx:systemctl start nginx.service
停止 Nginx:systemctl stop nginx.service
重启 Nginx:systemctl restart nginx.service
重载配置:systemctl reload nginx.service
设置开机自启:systemctl enable nginx.service
查看服务状态:systemctl status nginx.service

# 配置 nginx 开机自启
systemctl enable nginx.service

# 注意
如果需要自定义 Nginx 服务配置(如修改启动参数、重启策略),
正确做法是在 /etc/systemd/system/ 目录下创建同名文件(或创建 nginx.service.d/ 目录添加配置片段),
该目录的配置会覆盖 /lib/systemd/system/ 下的默认配置,且能避免软件更新时被覆盖。

卸载 nginx 源码包

shell
# 查看 nginx 进程,第二列是 PID
ps -ef | grep nginx

# 通过 PID 杀死 nginx 进程
kill -9 PID

# 删除 nginx 安装文件
rm -rf /usr/local/nginx

# 删除全局环境的 nginx 配置
vim /etc/profile
# 删除
PATH=$PATH:/usr/local/nginx/sbin
export PATH
# 保存并退出
source / etc / profile

# 修改 systemd 配置文件
vim /lib/systemd/system/nginx.service
# 删除
[Unit]

Description=nginx service

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s quit

PrivateTmp=true

[Install]

WantedBy=multi-user.target
# 保存并退出

nginx 自身常见命令

shell
ps -ef | grep nginx  # 查看 nginx 进程
kill -9 pid          # 杀掉 nginx 进程
nginx                # 启动nginx

nginx -s stop        # 立即停止nginx,不保存相关信息
nginx -s quit        # 正常退出nginx,并保存相关信息
killall nginx        # 杀死所有nginx进程

nginx -s reload      # 修改配置文件后,以优雅的方式重启Nginx
nginx -s reopen      # 重启Nginx

nginx -t             # 检测配置文件
nginx -V             # 查看 nginx 版本

安装方式二:RPM / yum(Linux)

安装 nginx(CentOS发行版)

shell
# 使用 yum 安装
yum -y install nginx

# 启动 nginx 服务
systemctl start nginx

# 查看 nginx 是否成功启动
nginx - t

重要文件位置

shell
/usr/share/nginx/html/index.html # html 根目录所在位置
/etc/nginx/nginx.conf # nginx 配置文件位置

# 注意:rpm 包安装的 nginx,在总配置文件 nginx.conf 中通过 include 的方式,将 server 块分割出去,成为子配置文件

systemctl 结合 nginx 的常用命令

shell
systemctl start nginx           # 启动服务
systemctl reload nginx          # 重新加载nginx.conf配置文件
systemctl restart nginx         # 重启服务
systemctl stop nginx            # 停止服务

systemctl enable nginx          # 开机自启
systemctl disable nginx         # 开机不自启
systemctl list-unit-files       # 检查nginx是否已经安装了开机自动启动

systemctl status nginx          # 查看nginx状态

ps -ef | grep nginx             # 查看进程nginx进程
kill -9 pid                     # 杀掉 nginx 进程
netstat -antlp | grep nginx     # 查看nginx服务端口

yum remove nginx                # yum 卸载nginx软件包

nginx 自身常见命令

shell
ps -ef | grep nginx  # 查看 nginx 进程
kill -9 pid          # 杀掉 nginx 进程
nginx                # 启动nginx

nginx -s stop        # 立即停止nginx,不保存相关信息
nginx -s quit        # 正常退出nginx,并保存相关信息
killall nginx        # 杀死所有nginx进程

nginx -s reload      # 修改配置文件后,以优雅的方式重启Nginx
nginx -s reopen      # 重启Nginx

nginx -t             # 检测配置文件
nginx -V             # 查看 nginx 版本

安装方式三:Windows

下载、安装、测试

shell
1. 访问 https://nginx.org/en/download.html 下载 nginx/Windows 前缀的文件
2. 直接解压到指定目录下,比如解压到 F 下,将目录名改成 nginx
3. nginx 目录下打开 cmd 终端,输入 start nginx
4. 在浏览器上输入:http://localhost 进行测试

配置全局命令

shell
1. 编辑系统环境变量 --> 环境变量 --> 系统变量,path --> 添加:F:/nginx
2. win + r 打开 cmd 终端,输入:nginx -t

配置 nginx 开机自启

shell
1. 点击 win 键,输入:任务 --> 选择任务计划程序 --> 打开左侧目录到 windows,即:任务计划程序库/Microsoft/Windows
2. 点击创建基本任务 --> 名称:nginx 自启,描述:nginx 开机自启,下一步 --> 计算机启动时,下一步 --> 默认启动程序,下一步
3. 输入程序或脚本:F:\nginx\nginx.exe (指定到 nginx 执行程序) --> 起始于:F:\nginx(指定到 nginx 执行目录)--> 下一步,完成
4. 单击刚配置号的 nginx 自启 --> 常规:勾选不管用户是否登录都要运行 --> 条件:只勾选,只有在以下网络连接可用时才启动
5. 设置:勾选前三个和倒数第二个 --> 确认,输入用户密码
6. 重启后在浏览器输入:http://localhost 测试
5. 关闭开机自启:点击创建的nginx自启任务 --> 右键删除 --> 确认

windows 系统下 nginx 常见命令

shell
start nginx       # 启动nginx

nginx -s stop     # 关闭nginx
nginx -s quit     # 关闭nginx

nginx -s reload   # 重启nginx
nginx -s reopen   # 重新打开nginx日志文件

nginx -t          # 查看nginx配置文件状态
nginx -v          # 查看nginx版本

附:nginx 进程与服务命令(汇总)

源码安装与 yum 安装路径不同,但 nginx -t / nginx -s reload / systemctl(若配置了 unit)含义一致;Windows 使用 start nginxnginx -s reload 等。详细仍以各小节为准。